package com.lcxw.hxzb.friendcircle.project.system.controller;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.lcxw.hxzb.friendcircle.common.utils.DateUtils;
import com.lcxw.hxzb.friendcircle.framework.web.controller.BaseController;
import com.lcxw.hxzb.friendcircle.framework.web.domain.AjaxResult;
import com.lcxw.hxzb.friendcircle.framework.web.page.TableDataInfo;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdLoves;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdRank;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdLovesService;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 情侣关系Controller
 *
 * @author ruoyi
 * @date 2020-09-27
 */
@RestController
@RequestMapping("/system/loves")
public class YwpdLovesController extends BaseController {
    @Autowired
    private IYwpdLovesService ywpdLovesService;
    @Autowired
    private IYwpdRankService ywpdRankService;

    /**
     * 查询情侣关系列表
     */
    @GetMapping("/list")
    public AjaxResult list(YwpdLoves ywpdLoves) {
        ywpdLoves.setStatus("1");
        List<YwpdLoves> listLoves = ywpdLovesService.selectYwpdLovesList(ywpdLoves);
        List<YwpdRank> rankList = ywpdRankService.selectYwpdRankList(null);
        for (YwpdLoves loves : listLoves) {
            BigDecimal lovesMoney = loves.getMoney();
            for (YwpdRank ywpdRank : rankList) {
                BigDecimal rankMoney = ywpdRank.getMoney();
                if (lovesMoney.compareTo(rankMoney)==1){
                    loves.setYwpdRank(ywpdRank);
                }
            }
        }

        return AjaxResult.success(listLoves);
    }

    /**
     * 获取情侣关系详细信息
     */
    @GetMapping(value = "/{lovesId}")
    public AjaxResult getInfo(@PathVariable("lovesId") Long lovesId) {
        return AjaxResult.success(ywpdLovesService.selectYwpdLovesById(lovesId));
    }

    /**
     * 新建情侣关系
     */
    @PostMapping
    public AjaxResult add(@RequestBody YwpdLoves ywpdLoves) {
        ywpdLoves.setStatus("0");
        ywpdLoves.setMoney(BigDecimal.valueOf(0));
        return toAjax(ywpdLovesService.insertYwpdLoves(ywpdLoves));
    }

    /**
     * 同意情侣关系
     */
    @PutMapping("/agree")
    public AjaxResult agree(@RequestBody YwpdLoves ywpdLoves) {
        ywpdLoves.setStatus("1");
        return toAjax(ywpdLovesService.updateYwpdLoves(ywpdLoves));
    }

    /**
     * 拒绝情侣关系
     */
    @PutMapping("/refuse")
    public AjaxResult refuse(@RequestBody YwpdLoves ywpdLoves) {
        ywpdLoves.setStatus("2");
        return toAjax(ywpdLovesService.updateYwpdLoves(ywpdLoves));
    }

    /**
     * 解散情侣关系
     */
    @PutMapping("/dissolution")
    public AjaxResult dissolution(@RequestBody YwpdLoves ywpdLoves) {
        ywpdLoves.setStatus("3");
        ywpdLoves.setTime(DateUtils.getNowDate());
        return toAjax(ywpdLovesService.updateYwpdLoves(ywpdLoves));
    }

    /**
     * 删除情侣关系
     */
    @DeleteMapping("/{lovesIds}")
    public AjaxResult remove(@PathVariable Long[] lovesIds) {
        return toAjax(ywpdLovesService.deleteYwpdLovesByIds(lovesIds));
    }
}
